//
//  ViewController.m
//  Net
//
//  Created by student on 16/3/24.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
{
    NSMutableData *mData;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/login?username=123&pwd=123&method=get&type=JSON"];
    NSLog(@"%@",url.scheme);
    NSLog(@"%@",url.host);
    NSLog(@"%@,",url.relativePath);
    NSLog(@"%@",url.path);
    NSLog(@"%@",url.port);
    NSLog(@"%@",url.query);
}
- (IBAction)Btn1Pressed:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/login?username=123&pwd=123&method=get&type=JSON"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLResponse *response;
    NSError *error;
    
    NSDate *d1 = [NSDate date];
   NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSDate *d2 = [NSDate date];
    NSTimeInterval t = [d2 timeIntervalSinceDate:d1];
    NSLog(@"花费了%.3f秒",t);
    
    NSHTTPURLResponse *httpResponse = response;
    NSLog(@"状态码 %i",httpResponse.statusCode);
    NSLog(@"响应头 %@",httpResponse.allHeaderFields);
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);
}
- (IBAction)btn2Pressed:(id)sender
{
    
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812//MJServer/login?username=123&pwd=123&method=get&type=JSON"];
    
   
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
  
    NSDate *d1 = [NSDate date];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"%@",str);
    }];
    
    NSDate *d2 = [NSDate date];
    NSTimeInterval t = [d2 timeIntervalSinceDate:d1];
    NSLog(@"花费了%.3f秒",t);
}

- (IBAction)btn3Pressed:(id)sender
{
    
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_02.png"];
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
   
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    
    
    mData = [NSMutableData data];
    
    [connection start];
    
}
- (IBAction)Btn4Pressed:(id)sender
{
    
    NSURL *url = [NSURL URLWithString:@"http://192.168.13.28:8080/MJServer/login"];
    
   
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    NSData *postData = [@"username=123&pwd=123&method=get&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];//NSString -> NSData
    request.HTTPBody = postData;
    
  
    NSDate *d1 = [NSDate date];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"%@",str);
    }];
    
    NSDate *d2 = [NSDate date];
    NSTimeInterval t = [d2 timeIntervalSinceDate:d1];
    NSLog(@"花费了%.3f秒",t);
}
	

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *h = response;
    NSLog(@"接收到服务器响应 %i %@",h.statusCode,h.allHeaderFields);
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"接收到服务器返回的数据%i",data.length);
    [mData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"接收完成，显示到界面");
    UIImage *img = [UIImage imageWithData:mData];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    [self.view addSubview:imgView];
    imgView.frame = CGRectMake(100, 200, img.size.width, img.size.height);
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
